package com.devenv.common;

import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;
import com.fasterxml.jackson.annotation.JsonInclude;

import java.time.LocalDateTime;

/**
 * 统一响应结果类
 * <p>
 * 用于统一API响应格式，包含状态码、消息、数据和时间戳等信息
 * </p>
 * 
 * @author DevEnv OpenSource Team
 * @version 1.0.0
 * @since 1.0.0
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Result<T> {
    
    private int code;
    private String message;
    private T data;
    private LocalDateTime timestamp;
    private String traceId; // 用于日志追踪
    private Object metadata; // 额外的元数据，如分页信息

    // 成功响应
    public static <T> Result<T> success() {
        return success(null);
    }

    public static <T> Result<T> success(T data) {
        return success(data, "操作成功");
    }

    public static <T> Result<T> success(T data, String message) {
        Result<T> result = new Result<>();
        result.code = 200;
        result.message = message;
        result.data = data;
        result.timestamp = LocalDateTime.now();
        return result;
    }

    // 错误响应
    public static <T> Result<T> error(String message) {
        return error(500, message);
    }

    public static <T> Result<T> error(int code, String message) {
        Result<T> result = new Result<>();
        result.code = code;
        result.message = message;
        result.timestamp = LocalDateTime.now();
        return result;
    }

    // 便捷方法
    public static <T> Result<T> notFound(String message) {
        return error(404, message != null ? message : "资源不存在");
    }

    public static <T> Result<T> badRequest(String message) {
        return error(400, message != null ? message : "请求参数错误");
    }

    public static <T> Result<T> forbidden(String message) {
        return error(403, message != null ? message : "无权限访问");
    }

    public static <T> Result<T> unauthorized(String message) {
        return error(401, message != null ? message : "未授权访问");
    }

    public static <T> Result<T> conflict(String message) {
        return error(409, message != null ? message : "资源冲突");
    }

    public static <T> Result<T> tooManyRequests(String message) {
        return error(429, message != null ? message : "请求过于频繁");
    }

    // 设置追踪ID
    public Result<T> withTraceId(String traceId) {
        this.traceId = traceId;
        return this;
    }

    // 设置元数据
    public Result<T> withMetadata(Object metadata) {
        this.metadata = metadata;
        return this;
    }

    // 判断是否成功
    public boolean isSuccess() {
        return this.code == 200;
    }

    // 判断是否失败
    public boolean isError() {
        return !isSuccess();
    }

    // 获取状态码描述
    public String getStatusDescription() {
        switch (code) {
            case 200: return "成功";
            case 400: return "请求错误";
            case 401: return "未授权";
            case 403: return "禁止访问";
            case 404: return "未找到";
            case 409: return "冲突";
            case 429: return "请求过多";
            case 500: return "服务器错误";
            default: return "未知状态";
        }
    }

    /**
     * 创建带分页信息的成功响应
     */
    public static <T> Result<T> successWithPagination(T data, long total, int page, int size) {
        PageInfo pageInfo = new PageInfo(total, page, size);
        return success(data).withMetadata(pageInfo);
    }

    /**
     * 分页信息类
     */
    @Data
    @AllArgsConstructor
    public static class PageInfo {
        private long total;
        private int page;
        private int size;
        private long totalPages;

        public PageInfo(long total, int page, int size) {
            this.total = total;
            this.page = page;
            this.size = size;
            this.totalPages = size > 0 ? (total + size - 1) / size : 0;
        }
    }
}
